package com.ruoyi.pump.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.dc3.api.center.manager.feign.DeviceClient;
import com.ruoyi.dc3.common.model.Device;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pump.domain.Dc3Device;
import com.ruoyi.pump.service.IDc3DeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * dc3设备信息Controller
 * 
 * @author ruoyi
 * @date 2025-09-18
 */
@RestController
@RequestMapping("/pump/dc3device")
public class Dc3DeviceController extends BaseController
{
    @Autowired
    private IDc3DeviceService dc3DeviceService;

    /**
     * 查询dc3设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('pump:dc3device:list')")
    @GetMapping("/list")
    public TableDataInfo list(Dc3Device dc3Device)
    {
        startPage();
        List<Dc3Device> list = dc3DeviceService.selectDc3DeviceList(dc3Device);
        return getDataTable(list);
    }

    /**
     * 导出dc3设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('pump:dc3device:export')")
    @Log(title = "dc3设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Dc3Device dc3Device)
    {
        List<Dc3Device> list = dc3DeviceService.selectDc3DeviceList(dc3Device);
        ExcelUtil<Dc3Device> util = new ExcelUtil<Dc3Device>(Dc3Device.class);
        util.exportExcel(response, list, "dc3设备信息数据");
    }

    /**
     * 获取dc3设备信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('pump:dc3device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dc3DeviceService.selectDc3DeviceById(id));
    }

    /**
     * 新增dc3设备信息
     */
    @PreAuthorize("@ss.hasPermi('pump:dc3device:add')")
    @Log(title = "dc3设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Dc3Device dc3Device)
    {
        return toAjax(dc3DeviceService.insertDc3Device(dc3Device));
    }

    /**
     * 修改dc3设备信息
     */
    @PreAuthorize("@ss.hasPermi('pump:dc3device:edit')")
    @Log(title = "dc3设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Dc3Device dc3Device)
    {
        return toAjax(dc3DeviceService.updateDc3Device(dc3Device));
    }

    /**
     * 删除dc3设备信息
     */
    @PreAuthorize("@ss.hasPermi('pump:dc3device:remove')")
    @Log(title = "dc3设备信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dc3DeviceService.deleteDc3DeviceByIds(ids));
    }



    @Autowired
    DeviceClient deviceClient;
    @PostMapping("/sync")
    public AjaxResult sync() {

        dc3DeviceService.selectDc3DeviceList(new Dc3Device()).forEach(dc3Device -> {
            dc3Device.setSync("N");
            dc3DeviceService.updateDc3Device(dc3Device);
        });

        List<Device> devices = deviceClient.all().getData();
        devices.forEach(device -> {
            Dc3Device dcDevice = new Dc3Device();
            dcDevice.setId(device.getId());
            dcDevice.setName(device.getName());
            dcDevice.setRemark(device.getDescription());
            dcDevice.setSync("Y");
            if (getInfo(device.getId()).containsKey("data")) {
                edit(dcDevice);
            }else{
                add(dcDevice);
            }
        });
        return AjaxResult.success("同步成功");
    }

}
